import * as Base64 from 'js-base64'
import { isString } from 'lodash-es'
import { Data } from '../index';


export const base64Encode = (code: string | Data): string => {
  if (isString(code)) {
    return Base64.encode(code)
  } else {
    return Base64.encode(JSON.stringify(code))
  }
}

export const base64Decode = (code: string): any => {
  const val = Base64.decode(code)
  try {
    return JSON.parse(val)
  } catch {
    return val
  }
}
